import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 打开浏览器
service = Service(r'D:\Python3.14\chromedriver.exe')
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service, options=chrome_options)   # 创建浏览器对象
# 输入访问网址
driver.get("http://121.43.169.97:8848/pageA.html")

"""
需求：使用‘注册A.html’页面，完成对城市的下拉框的操作
python基础).选择‘广州’
2).暂停2秒，选择‘上海’
3).暂停2秒，选择‘北京‘
"""
# 方法一定位到要操作的option元素，然后执行点击操作
# driver.find_element(By.CSS_SELECTOR,  "[value='gz']").click()
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,  "[value='sh']").click()
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,  "[value='bj']").click()

# 方法二Select类实现
# 实例化Select类
select = Select(driver.find_element(By.ID, "selectA"))
# 根据索引实现
time.sleep(2)
select.select_by_index(2)
# 根据value属性实现
time.sleep(2)
select.select_by_value("sh")
# 根据文本内容实现
time.sleep(2)
select.select_by_visible_text("北京")
